package com.kilon.workfaq.rest;

import com.github.pagehelper.PageInfo;
import com.kilon.workfaq.entity.PostPo;
import com.kilon.workfaq.service.read.PostReadService;
import com.kilon.workfaq.service.write.PostWriteService;
import com.kilon.workfaq.util.ResultBean;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 博客文章表(Post)表控制层
 *
 * @author makejava
 * @since 2023-02-23 02:18:57
 */
@RestController
@RequestMapping("post")
//@CrossOrigin(origins = {"http://localhost:8080", "http://127.0.0.1:8080", "http://127.0.0.1","http://192.168.39.103:8899","http://localhost"}) //解决跨域问题

public class PostRestService extends ResultBean {
    /**
     * 服务对象
     */
    @Resource
    private PostReadService postReadService;
    @Resource
    private PostWriteService postWriteService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("selectOne")
    public PostPo selectOne(@RequestBody String id) {
        return this.postReadService.queryById(id);
    }

    @PostMapping("queryAll")
    public List<PostPo> selectAll(@RequestBody PostPo postPo) {
        if ("0".equals(postPo.getCategoryId())) {
            postPo.setCategoryId(null);
        }
        return this.postReadService.queryAll(postPo);
    }
    @PostMapping("selectAllpage")
    public PageInfo<PostPo> selectAllpage(@RequestBody PostPo postPo) {
        if ("0".equals(postPo.getCategoryId())) {
            postPo.setCategoryId(null);
        }
        return this.postReadService.queryAll(postPo,postPo.getPageCode(), postPo.getPageSize());
    }

    /**
     * 新增项目
     *
     * @return
     */
    @PostMapping("addPost")
    public ResultBean addPost(@RequestBody PostPo postPo) {
        ResultBean rb = new ResultBean(200,"新增成功");
        if (postPo.getTitle().contains("kilon")) {
            postPo.setTitle(postPo.getTitle().replace("kilon",""));
            if ("0".equals(postPo.getCategoryId())) {
                rb.setCode(400);
                rb.setMessage("所属系统不允许选择所有");
                return rb;
            }
            if ("".equals(postPo.getImageurl())) {
                postPo.setImageurl("https://jmgc.mzt.ln.gov.cn/faq/images/default.png");
            }
            //这里添加自定义条件
            this.postWriteService.insert(postPo);
        }
        return rb;
    }

    /**
     * 更新项目
     *
     * @return
     */
    @PostMapping("updateProject")
    public void updatePost(@RequestBody PostPo postPo) {
        //这里添加自定义条件
        if (this.postWriteService.update(postPo)) {
            //setResultData("更新成功");
        } else {
            //setErrorMsg("更新出错");
        }
    }

    /**
     * 删除项目
     *
     * @return
     */
    @PostMapping("dropPost")
    public void dropPost(@RequestBody PostPo postPo) {
        if (this.postWriteService.deleteById(postPo)) {
            //setResultData("删除成功");
        } else {
            //setErrorMsg("删除出错");
        }
    }
}
